home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Learn C++ (CodeWarrior)
/
Chap 09.06 - memberWise
/
memberWise.cp
< prev
next >
Wrap
Text File
|
1995-10-21
|
2KB
|
88 lines
#include <iostream.h>
#include <string.h>
//--------------------------------------- Name
class Name
{
private:
char *last;
char *first;
public:
Name( char *firstParam, char *lastParam );
Name( const Name &original );
~Name();
void Display();
Name &operator=( const Name &original );
};
Name::Name( char *firstParam, char *lastParam )
{
first = new char[ strlen(firstParam) + 1 ];
last = new char[ strlen(lastParam) + 1 ];
strcpy( first, firstParam );
strcpy( last, lastParam );
cout << "Original constructor...\n";
}
Name::Name( const Name &original )
{
first = new char[ strlen(original.first) + 1 ];
last = new char[ strlen(original.last) + 1 ];
strcpy( first, original.first );
strcpy( last, original.last );
cout << "Copy constructor...\n";
}
Name::~Name()
{
delete [] first;
delete [] last;
}
void Name::Display()
{
cout << "Name: " << first << " " << last << "\n";
}
Name &Name::operator=( const Name &original )
{
if ( this == &original )
return( *this );
delete [] first;
delete [] last;
first = new char[ strlen(original.first) + 1 ];
last = new char[ strlen(original.last) + 1 ];
strcpy( first, original.first );
strcpy( last, original.last );
return( *this );
}
//--------------------------------------- main()
int main()
{
Name yourAuthor( "Dave", "Mark" );
Name aCopy = yourAuthor;
Name anotherAuthor( "Scott", "Knaster" );
yourAuthor.Display();
aCopy.Display();
aCopy = anotherAuthor;
aCopy.Display();
return 0;
}